﻿using CustomControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestFor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        List<MyLED> list = new List<MyLED>();

        /// <summary>
        /// 数组初始化
        /// </summary>
        private void ListInit()
        {
            list.Add(myLED1);
            list.Add(myLED2);
            list.Add(myLED3);
            list.Add(myLED4);
            list.Add(myLED5);
            list.Add(myLED6);
        }

        /// <summary>
        /// LED灯状态初始化
        /// </summary>
        /// <param name="myLEDs"></param>
        private void LEDInit(List<MyLED> myLEDs)
        {
            for (int i = 0; i < myLEDs.Count; i++)
            {
                myLEDs[i].IsFlash = false;
                myLEDs[i].LedStatus = 0;
            }
        }

        /// <summary>
        /// LED灯根据bool值进行改变状态的方法
        /// </summary>
        /// <param name="ScrewOKorNG"></param>
        /// <returns></returns>
        private bool LED(bool ScrewBusy ,bool ScrewOKorNG)
        {
            //循环判断此时的LED灯的状态，如果第一个灯已经有状态的，
            //则跳过这个灯，去判断下一个灯的状态，直到找到有个灯的状态是初始化状态，
            //则改变对应的状态，然后返回
            for (int i = 0; i < list.Count+1; i++)
            {
                //判断是否大于或者等于数组，如果大于等于数组，则需要进行所有LED灯状态进行初始化
                if (i >= list.Count || list[i].LedStatus != 0)
                {
                    //并且判断得到是OK还是NG，并改变数组第一个灯的状态
                    if (i >= list.Count)
                        {
                            this.LEDInit(list);
                            if (ScrewOKorNG) //在最后一次点击的时候会显示状态
                            {
                                list[0].IsFlash = false;
                                list[0].LedStatus = 1;
                                return true;
                            }
                            else if (!ScrewOKorNG)
                            {
                                list[0].IsFlash = false;

                                list[0].LedStatus = 2;
                                return true;
                            }
                        }
                    continue;
                }
                if (ScrewBusy)
                {
                    list[i].IsFlash = true;
                    return true;
                }
                else
                {
                    if (ScrewOKorNG) //在最后一次点击的时候会显示状态
                    {
                        list[i].IsFlash = false;
                        list[i].LedStatus = 1;
                        return true;
                    }
                    else if (!ScrewOKorNG)
                    {
                        list[i].IsFlash = false;
                        list[i].LedStatus = 2;
                        return true;
                    }
                }
            }
            return false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ListInit();
        }
         bool ScrewNG = false;

        private void button1_Click(object sender, EventArgs e)
        {
            ScrewBusy = false;
            LED(ScrewBusy,ScrewNG);
        }

        /// <summary>
        /// 点击使LED灯状态初始化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            LEDInit(list);
        }
        bool ScrewBusy;
        private void button3_Click(object sender, EventArgs e)
        {
            ScrewBusy = true;
            LED(ScrewBusy, ScrewNG);
        }
    }
}
